package com.panfeng.xcloud.boss.provider.assets.web.rpc;

import com.alibaba.fastjson.JSON;
import com.panfeng.xcloud.boss.provider.assets.service.IExecuteOrderService;
import com.panfeng.xcloud.boss.provider.dto.request.OperateBotReqDTO;
import com.panfeng.xcloud.boss.provider.dto.request.SendEmailReqDTO;
import com.panfeng.xcloud.boss.provider.rpc.assets.ExecuteOrderServiceFeignApi;
import com.panfeng.xcloud.boss.provider.rpc.member.UserInfoFeignApi;
import com.panfeng.xcloud.common.core.configuration.DataDictionaryConfig;
import com.panfeng.xcloud.common.core.web.vo.OrderDetailResponseVO;
import com.panfeng.xcloud.common.core.web.vo.ResponseVO;
import com.panfeng.xcloud.common.security.annotation.NotNeedAccess;
import com.panfeng.xcloud.common.security.annotation.NotNeedFeignAccess;
import com.panfeng.xcloud.common.security.web.support.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Slf4j
@RestController
@Api(value = "API - ExecuteOrderServiceApiFeignClient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ExecuteOrderServiceApiFeignClient extends BaseController implements ExecuteOrderServiceFeignApi {

    @Autowired
    private IExecuteOrderService executeOrderService;

    @Autowired
    private DataDictionaryConfig dataDictionaryConfig;

    @Autowired
    private UserInfoFeignApi userInfoFeignApi;

    @NotNeedAccess
    @NotNeedFeignAccess
    @PostMapping(value = "/feignApi/assets/chooseSendOrderHttp", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(httpMethod = "POST", value = "rest-rpc初始化-chooseSendOrderHttp操作")
    @Override
    public ResponseVO chooseSendOrderHttp(@RequestBody OperateBotReqDTO operateBotReqDTO, @RequestParam(value = "systemInnerType") Integer systemInnerType) {
        try {
            log.info(">>> chooseSendOrderHttp:{} <<<<", (null != operateBotReqDTO ? JSON.toJSONString(operateBotReqDTO) : ""));
            OrderDetailResponseVO orderDetail = executeOrderService.chooseSendOrderHttp(operateBotReqDTO, systemInnerType);
            return ResponseVO.with(orderDetail);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String message = "超级量化正式版下单时报错返回值发信息   用户id:" + operateBotReqDTO.getUserId()
                    + "   方向:" + operateBotReqDTO.getDirection()
                    + "   倍数:" + operateBotReqDTO.getLeverRate()
                    + "   数量:" + operateBotReqDTO.getAmount()
                    + "   目的币种:" + operateBotReqDTO.getDestinationCoin()
                    + "   操作类型:" + operateBotReqDTO.getOperateType()
                    + "   异常信息:" + com.panfeng.xcloud.common.core.utils.StringUtils.printStackTraceToString(e);
            userInfoFeignApi.sendEmail(new SendEmailReqDTO(message));
            return ResponseVO.with(ResponseVO.FAIL, ResponseVO.FAIL, e);
        }
    }

    @NotNeedAccess
    @NotNeedFeignAccess
    @PostMapping(value = "/feignApi/assets/toSetOpenOrderAmount", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(httpMethod = "POST", value = "rest-rpc初始化-toSetOpenOrderAmount操作")
    @Override
    public ResponseVO toSetOpenOrderAmount(@RequestBody OperateBotReqDTO operateBotReqDTO, @RequestParam(value = "amount") BigDecimal amount) {
        try {
            log.info(">>> toSetOpenOrderAmount:{} <<<<", (null != operateBotReqDTO ? JSON.toJSONString(operateBotReqDTO) : ""));
            BigDecimal realAmount = executeOrderService.toSetOpenOrderAmount(operateBotReqDTO, amount);
            return ResponseVO.with(realAmount);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseVO.with(ResponseVO.FAIL, ResponseVO.FAIL, e);
        }
    }

}
